import CommonAction from "../../../actions/common";

export default class HomeAction extends CommonAction {
    constructor(comp) {
        super();
        this.comp = comp;
        this.dispatch = comp.props.dispatch
    }

    // 初始化
    didMount = () => {
        // 获取人数统计
        this.getStaffStatic();
    };

    // 卸载
    willUnMount = () => {

    };

    hrorgChange = async (val) => {
        let pk_org = val && val.map(item => {
            return item.refpk
        })

        // let pk_org = (val && val.refpk) || null;
        await this.dispatch({
            type: 'staffCom/update',
            payload: {
                hrorgobj: val,
                pk_org
            }
        });
        this.getStaffStatic({pk_org})
    };

    psnclChange = async (val) => {
        // let pk_psncl = (val && val.refpk) || null;
        let pk_psncl = val && val.map(item => {
            return item.refpk
        })

        await this.dispatch({
            type: 'staffCom/update',
            payload: {
                psncobj: val,
                pk_psncl
            }
        });
        this.getStaffStatic({pk_psncl})
    };

    // 获取人数统计
    getStaffStatic = async ({pk_org, pk_psncl} = {}) => {
        const {props} = this.comp;
        const {staffCom} = props;
        !pk_org && (pk_org = staffCom.pk_org);
        !pk_psncl && (pk_psncl = staffCom.pk_psncl);
        let postData = {
            pk_org,
            pk_psncl
        };
        try {
            let res = await this.dispatch({
                type: 'staffCom/getStaffStatic',
                payload: {
                    postData
                }
            });
            if (!res.success) return;
            // 设置banner数据
            if (res.data) {
                let staticData = this.comp.state.staticData;
                staticData.map(item => {
                    let key = item.key;
                    if (res.data[key]) {
                        item.num = res.data[key];
                    }
                });
                this.comp.setState({
                    staticData
                })
            }
            // 设置图表数据
        } catch (e) {
            console.error(e)
        }
    }

    showDetail = (page) => {
        this.dispatch({
            type: 'staffCom/update',
            payload: {
                page
            }
        });
    }
}